package com.zhoug.ocr.textin.http.core;



import com.zhoug.ocr.textin.TextInConfig;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @Author: zhoug
 * @Date: 2023/2/20
 * @Description:
 */
public class TextInTokenInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        request = addParam(request);
        return chain.proceed(request);
    }



    /**
     * 添加统一参数在url上
     *
     * @param oldRequest
     * @return
     */
    private Request addParam(Request oldRequest) {
        //添加通用参数到url中
//        HttpUrl.Builder builder = oldRequest.url()
//                .newBuilder()
//                .setEncodedQueryParameter("version", "" + AppManager.getInstance().getVersion())//通用参数版本号
//                .setEncodedQueryParameter("platform", "android");//平台:android
        Request.Builder newBuilder = oldRequest.newBuilder();
        //添加token到header中
        //添加通用参数
        //添加ClientType
        newBuilder.removeHeader(TextInConfig.X_TI_APP_ID_KEY);//防止添加多个同名
        newBuilder.addHeader(TextInConfig.X_TI_APP_ID_KEY, TextInConfig.getX_ti_app_id());
        newBuilder.removeHeader(TextInConfig.X_TI_SECRET_CODE_KEY);//防止添加多个同名
        newBuilder.addHeader(TextInConfig.X_TI_SECRET_CODE_KEY, TextInConfig.getX_ti_secret_code());
        //创建新请求
        return newBuilder.method(oldRequest.method(), oldRequest.body())
                .url(oldRequest.url())
                .build();
    }
}
